let {Contact} = require('wechaty');
let get_weather = require('../tool/weather');
let user_filter = require('../tool/user_filter');

module.exports = function (message) {

    // 自己发的消息不处理
    // 白名单里的消息才处理
    if (!message.self() && user_filter(message)) {
        let name = message.from().name();
        let alias = message.from().alias();
        let content = message.content();

        console.log('%s:%s', name, content);
        if(alias === 'feifei' && !content.startsWith('#')){
            Contact.find({alias: "chrome"})
                .then(contact => {
                    contact.say('你老婆说：' + content)
                    return 'OK';
                });
        }

        if(alias === 'chrome' && (content.startsWith('-') || content.startsWith('+'))){
            Contact.find({alias: "feifei"})
                .then(contact => {
                    if(content.startsWith('+')){
                        contact.say('你老公说：' + content.replace('+', ''))
                    }else if(content.startsWith('-')){
                        contact.say(content.replace('-', ''))
                    }
                    return 'OK';
                });
            return;
        }

        if (content.indexOf('#天气') === 0) {
            message.say('正在给你查询天气。。。');

            let city = content.replace('#天气', '').trim();
            if(!city || city.length === 0){
                let date = new Date();
                let hour = date.getHours();// 0-23
                let week = date.getDay();// 0-6,0代表星期天
                if(0 < week < 6){// 工作日
                    if(hour <= 10 || hour >= 19){// 在家
                        city = '昌平区';
                    }else if(hour < 19){// 在公司
                        city = '北京';
                    }
                }else{// 周末
                    city = '昌平区';
                }
            }
            get_weather(city, function (error, response, body) {
                if(error){
                    console.error('查询天气出错：'+error.message);
                }
                if (!error && response.statusCode === 200) {
                    let info = JSON.parse(body);
                    //console.log(info);
                    if(info.status === '0'){
                        let msg =
                            "日期：" + info.result.date + " " + info.result.week + "\n"
                            + "城市：" + info.result.city + "\n"
                            + "天气：" + info.result.weather + "\n"
                            + "气温：" + info.result.templow + "~" + info.result.temphigh + "\n"
                            + "风力：" + info.result.winddirect + info.result.windpower + "\n"
                            + info.result.updatetime + "\n"
                            + "更多天气：http://beijing.tianqi.com/";
                        message.say(msg);
                    }else{
                        message.say('没查询到天气吆！自己去查询吧：http://beijing.tianqi.com/');
                    }

                } else {
                    message.say('没查询到天气吆！自己去查询吧：http://beijing.tianqi.com/');
                }
            });
        }
    }
};